<template>
  <PageWrapper contentBackground title="菜单管理" content="菜单管理，对菜单的增删改查操作">
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增菜单 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column['key'] === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'ant-design:form-outlined',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:control-outlined',
                onClick: handleComponentEdit.bind(null, record),
              },
              {
                icon: 'ant-design:api-outlined',
                onClick: handleApiEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <MenuComponentModal @register="registerComponentModal" @success="handleComponentSuccess" />
    <MenuApiModal @register="registerApiModal" @success="handleApiSuccess" />
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, nextTick } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { queryMenuTreeList, removeMenuById } from '/@/api/system/system';

  import { useDrawer } from '/@/components/Drawer';
  import MenuDrawer from './MenuDrawer.vue';

  import { columns, searchFormSchema } from './menu.data';
  import { PageWrapper } from '/@/components/Page';
  import { MenuReq } from '/@/api/system/modal/menuModel';
  import MenuComponentModal from '/@/views/system/menu/MenuComponentModal.vue';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import MenuApiModal from '/@/views/system/menu/MenuApiModal.vue';

  export default defineComponent({
    name: 'MenuManagement',
    components: {
      BasicTable,
      MenuDrawer,
      TableAction,
      PageWrapper,
      MenuComponentModal,
      MenuApiModal,
    },
    setup() {
      const { createMessage } = useMessage();
      const [registerComponentModal, { openModal: openComponentModal }] = useModal();
      const [registerApiModal, { openModal: openApiModal }] = useModal();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload, expandAll }] = useTable({
        title: '菜单列表',
        api: queryMenuTreeList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        isTreeTable: true,
        pagination: false,
        striped: false,
        useSearchForm: false,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        actionColumn: {
          width: 160,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleComponentEdit(record: Recordable) {
        openComponentModal(true, {
          record,
          isUpdate: true,
        });
      }

      function handleComponentSuccess() {
        reload();
      }

      function handleApiEdit(record: Recordable) {
        openApiModal(true, {
          record,
          isUpdate: true,
        });
      }

      function handleApiSuccess() {
        reload();
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleSuccess() {
        reload();
      }

      async function handleDelete(record: Recordable) {
        try {
          const menuReq: MenuReq = {
            id: record?.id,
          };
          await removeMenuById(menuReq);
        } catch (error: any) {
          const { response, message } = error || {};
          const msg: string = response?.data?.message ?? message;
          createMessage.error(msg);
        }
        handleSuccess();
      }

      function onFetchSuccess() {
        // 演示默认展开所有表项
        nextTick(expandAll);
      }

      return {
        registerTable,
        registerComponentModal,
        registerApiModal,
        registerDrawer,
        handleCreate,
        handleComponentEdit,
        handleComponentSuccess,
        handleApiEdit,
        handleApiSuccess,
        handleEdit,
        handleSuccess,
        handleDelete,
        onFetchSuccess,
      };
    },
  });
</script>
